clear all 
set more off


* figure 6: self-emp vs unemployment in flows/wages in wage work.


* graph colors and such
set autotabgraphs on

local msize1 "medsmall"
local msize2 "medlarge"

local color1 "0 76 167"
local color2 "146 0 0"
local color3 "62 150 81"
local colorgb "white"

local color1b "0 76 167"
local color2b "146 0 0"
local color3b "62 150 81"


* load data
use "Transitions_BWUN_JJ.dta", clear 


* normalize by BW flows
gen _hold = ytrate if trans == "BW"
bys ccode year: egen ytrate_BW = max(_hold)
drop _hold
gen relytrate_BW = ytrate/ytrate_BW

* cut off at 30 just to make the graph nicer.
replace relytrate_BW = . if relytrate_BW > 30

* averages
bys ccode trans: egen arelytrate_BW = mean(relytrate_BW)
bys ccode trans: egen aAGDP = mean(AGDP)

* trends
reg relytrate_BW lcgdp if trans == "UW"
predict trend_rUW

* counters
sort ccode year trans
by ccode: gen counter = _n
replace counter = . if counter >= 17
replace counter = 1 if !missing(counter)

// U-->W / B-->W
#delimit ;
twoway
(scatter relytrate AGDP if trans == "UW", mcolor("`color1'%10") msize(`msize1') msymbol(O) mlwidth(none)) 
(line trend_rUW AGDP, lcolor(gs6) lwidth(medthin) alc(background)) 
(scatter arelytrate_BW aAGDP if trans == "UW" & counter == 1, mcolor("`color1'") msize(`msize2') mlabel(ccode) msymbol(O) mlabcolor("`color1'")) 
, name(u) legend(off) 
graphregion(color("`colorgb'")) plotregion(color(none)) bgcolor(none) xtitle("GDP per capita") 
yscale(r(0 30)) ylabel(0(10)30) xscale(log) xlabel(4000 8000 16000 32000 64000);	
graph export "Figures/Figure6A.tif", replace; 
graph export "Figures/pdf_figures/Figure6A.pdf", replace; 

#delimit cr





* --------- Add to Table 6

// U
reg relytrate lcgdp if trans == "UW", cluster(ccode)
sum relytrate if trans == "UW"

#delimit ;
outreg2 using "Figures/Table6A.tex", replace
keep(lcgdp) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("$\frac{ \text{Unemployed}}{\text{Self-employed}}$")
nonotes;
#delimit cr	

// Inactive
reg relytrate_BW lcgdp if trans == "NW", cluster(ccode)
sum relytrate_BW if trans == "NW"

#delimit ;
outreg2 using "Figures/Table6A.tex", append
keep(lcgdp) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("$\frac{ \text{Inactive}}{\text{Self-employed}}$")
nonotes;
#delimit cr	



* -----------------------------------------------------------------
* ------------------- NOW DO WAGES
* ----------------------------------------------------------------

use "AvgWage_byTransition.dta", clear

// marker for W-W wages
gen _hold = wage_t1 if trans == "WW"
bys ccode year: egen wage_t1_WW = max(_hold)
drop _hold

gen relwagew_t1 = wage_t1/wage_t1_WW

// normalize by self-emp --> wage work
gen _hold = wage_t1 if trans == "BW"
bys ccode year: egen wage_t1_BW = max(_hold)
drop _hold
gen relwageb_t1 = wage_t1/wage_t1_BW

// cut some outliers to make the graphs prettier
replace relwagew_t1 = . if relwagew_t1 > 1.35 | relwagew_t1 < 0.25

// averages
bys ccode trans: egen arelwagew = mean(relwagew_t1)
bys ccode trans: egen arelwageb = mean(relwageb_t1)
bys ccode trans: egen aAGDP = mean(AGDP)

// trends

local xtlist2 "BW UW NW"
local ccc = 4

foreach x of local xtlist2 {
	reg relwagew_t1 lcgdp if trans == "`x'"
	predict trendw_`x'

	reg relwageb_t1 lcgdp if trans == "`x'"
	predict trendb_`x'	
}

// counter
sort ccode year trans
by ccode: gen counter = _n
replace counter = . if counter > `ccc'
replace counter = 1 if !missing(counter)


gsort+ AGDP
#delimit ;
twoway
(scatter relwagew_t1 AGDP if trans == "BW", mcolor("`color1'%10") msize(`msize1') msymbol(O) mlwidth(none)) 
(scatter relwagew_t1 AGDP if trans == "UW", mcolor("`color2'%10") msize(`msize1') msymbol(D) mlwidth(none)) 
(line trendw_BW AGDP, lcolor("`color1'%50") lwidth(medthin) alc(background) lpattern(dash)) 
(line trendw_UW AGDP, lcolor("`color2'%50") lwidth(medthin) alc(background)) 
(scatter arelwagew aAGDP if trans == "BW" & counter == 1, mcolor("`color1'") msize(`msize2') mlabel(ccode) msymbol(O) mlabcolor("`color1'")) 
(scatter arelwagew aAGDP if trans == "UW" & counter == 1, mcolor("`color2'") msize(`msize2') mlabel(ccode) msymbol(D) mlabcolor("`color2'")) 
, name(ub_w) 
legend(order(5 3 6 4) label(5 "Self-Emp/Wage") label(6 "Unemployed/Wage") label(3 "fitted") label(4 "fitted") col(2)) 
graphregion(color("`colorgb'")) plotregion(color(none)) bgcolor(none) xtitle("GDP per capita") 
yscale(r(0 1.25)) ylabel(0(.25)1.25) xscale(log) xlabel(4000 8000 16000 32000 64000);	
graph export "Figures/Figure6B.tif", replace; 
graph export "Figures/pdf_figures/Figure6B.pdf", replace; 
#delimit cr



* ----------- regressions for tables

replace relwageb_t1 = . if relwageb_t1 > 3 

// U
reg relwageb_t1 lcgdp if trans == "UW", cluster(ccode)
sum relwageb_t1 if trans == "UW"

#delimit ;
outreg2 using "Figures/Table6B.tex", replace
keep(lcgdp) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("$\frac{ \text{Unemployed}}{\text{Self-employed}}$")
nonotes;
#delimit cr	

// Inactive
reg relwageb_t1 lcgdp if trans == "NW", cluster(ccode)
sum relwageb_t1 if trans == "NW"

#delimit ;
outreg2 using "Figures/Table6B.tex", append
keep(lcgdp) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("$\frac{ \text{Inactive}}{\text{Self-employed}}$")
nonotes;
#delimit cr	





